<?php

/**
 * Description of Generic
 *
 * @author Kam
 */
class My_Form_Frontened extends My_Form {

    public function loadDefaultDecorators() {
        $groupDecorator = array(
            'FormElements',
            array('Fieldset'), //, array('class' => 'span12')),
        );
        $ds = $this->getDisplayGroups();
        foreach ($ds as $dk => $d) {
            $this->getDisplayGroup($dk)->setDecorators($groupDecorator);
        }
        $elems = $this->getElements();
        /* @var $e Zend_Form_Element */
        foreach ($elems as $e) {

            $css = $e->getAttrib('class');
            $isBig = false;
            if (stripos($css, 'element-big') !== false) {
                $labelSpan = 'span2';
                $dataSpan = 'span10';
                $isBig = true;
            } else {
                $labelSpan = 'span4';
                $dataSpan = 'span8';
            }
            $spanRow = '';
            $isControlGroupCSS = true;
            $elementType = $this->getElementType($e);
            $class = 'type-' . $elementType . ' element-' . $e->getId();
            if ($e->hasErrors()) {
                $class .= ' error';
            }

            $decorators = array();
            if ($e instanceof My_Form_Element_ImageUpload) {
                $decorators[] = 'ImageUpload';
            } elseif ($e instanceof Zend_Form_Element_File) {
                $decorators[] = 'File';
            } else {
                $decorators[] = 'ViewHelper';
            }
            $elementCss = $e->getAttrib('class');
            $e->setAttrib('class', '');

            if ($e instanceof Zend_Form_Element_Button || $e instanceof Zend_Form_Element_Submit) {
                $e->setAttrib('class', 'form_button button ' . $e->getAttrib('class'));
            }
            if ($e instanceof Zend_Form_Element_Submit) {
                $e->setAttrib('class', 'btn btn-primary btn-large submit ' . $e->getAttrib('class'));
                if ($this->_addFormCancel) {
                    $decorators[] = array('FormCancel', $this->_formCancelOptions);
                }
                $isControlGroupCSS = false;
                $spanRow = '';
                $labelSpan = 'span1';
                $dataSpan = 'span12';
                $isBig = true;
            }
            $decorators[] = array('Description', array('tag' => 'div', 'class' => 'description'));
            $decorators[] = 'Errors';
            $decorators[] = array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => ($isControlGroupCSS ? 'controls ' : '') . $dataSpan . ' data type-' . $elementType . ' element-' . $e->getId()));
            if ($this->_showLabels) {
                if ($e instanceof Zend_Form_Element_Button || $e instanceof Zend_Form_Element_Submit) {
                    $decorators[] = array('Label', array('tag' => 'div', 'class' => 'label-hidden', 'tagClass' => 'label-column ' . ($isControlGroupCSS ? 'control-label' : '')));
                } else {
                    $decorators[] = array('Label', array('tag' => 'div', 'class' => 'label', 'tagClass' => $labelSpan . ' label-column ' . ($isControlGroupCSS ? 'control-label' : '')));
                }
            }
            $decorators[] = array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => ($isControlGroupCSS ? 'control-group ' : '' ) . ($isBig ? 'span12' : 'span6') . ' row-fluid ' . $spanRow . ' row-type-' . $elementType . ' row-' . $e->getId() . ' ' . str_replace(array('btn-primary', 'btn-large', 'btn-normal', 'btn-mini', 'btn-small', 'btn'), '', $elementCss)));
            $e->setDecorators($decorators);
            $css = $e->getAttrib('class');
            if (!($e instanceof Zend_Form_Element_Button || $e instanceof Zend_Form_Element_Submit)) {
                $e->setAttrib('class', $css . ' span12');
            }
        }
        $class = $this->getAttrib('class');
        if (!$class) {
            $class = '';
        }
        $this->setAttrib('class', 'form ' . $this->_mainCss . ' ' . $class);
        $formTitleCss = empty($this->_mainCss) ? '' : $this->_mainCss . '-title';
        $this->setDecorators(array(
            array('FormErrors', array(
                    'ignoreSubForms' => false,
                    'showCustomFormErrors' => true,
                    'onlyCustomFormErrors' => true,
                    'markupElementLabelEnd' => '</b>',
                    'markupElementLabelStart' => '<b>',
                    'markupListItemEnd' => '',
                    'markupListItemStart' => '',
                    'markupListStart' => '<div class="form-errors">',
                    'markupListEnd' => '</div>',
                )
            ),
            'FormElements',
            array('HtmlTag', array('tag' => 'div', 'class' => 'form_elements row-fluid')),
            'form',
            array('FormTitle', array('class' => $formTitleCss)),
        ));
    }

    public function render(Zend_View_Interface $view = null) {
        $elements = $this->getElements();
        /* @var $e Zend_Form_Element */
        foreach ($elements as $e) {
            $decorator = $e->getDecorator('row');
            if ($decorator) {
                $class = $decorator->getOption('class');
                if ($e->hasErrors()) {
                    $class .= ' error';
                } else {
                    $class .= ' info';
                }
                $decorator->setOption('class', $class);
            }
        }
        return parent::render($view);
    }

}
